package network;

import gui.MainPanel;
import gui.SuperUserPanel;

import java.util.concurrent.ExecutionException;

import javax.swing.JPanel;
import javax.swing.SwingWorker;

import data.Order;
import data.SaveResponse;
import data.Order.OrderState;


public class SaveOrderWorker extends SwingWorker<SaveResponse, Object> {

	JPanel mMainPanel = null;
	Order mOrder = null;
	
	public SaveOrderWorker(JPanel parent, Order order) {
		mMainPanel = parent;
		mOrder = order;
	}
	
	@Override
	protected SaveResponse doInBackground() throws Exception {
		return NetworkManager.getInstance().saveOrder(mOrder);
	}

	@Override
	protected void done() {
		SaveResponse status = null;
		try {
			status = get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (mMainPanel instanceof MainPanel)
				((MainPanel)mMainPanel).saved(status, mOrder.getState() == OrderState.Submitted);
			else if (mMainPanel instanceof SuperUserPanel)
				((SuperUserPanel)mMainPanel).saved(status, mOrder.getState() == OrderState.Accepted);
		}
		super.done();
	}
}
